package com.github.cdisk.xtrans.ui.toolbox;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import com.github.cdisk.xtrans.connect.Connect;
import com.github.cdisk.xtrans.ui.Operation;

public class ConnectTable extends Table implements SelectionListener {

	public ConnectTable(TabFolder parent, int style) {
		super(parent, style);
		init();
	}

	public void init() {
		FormData fd_table = new FormData();
		fd_table.bottom = new FormAttachment(100);
		fd_table.left = new FormAttachment(0);
		fd_table.right = new FormAttachment(0, 152);
		fd_table.top = new FormAttachment(0, 28);
		this.setLayoutData(fd_table);
		this.setHeaderVisible(true);
		this.setLinesVisible(true);

		TableColumn tblclmnName = new TableColumn(this, SWT.NONE);
		tblclmnName.setWidth(100);
		tblclmnName.setText("连接名称");

		TableColumn tblclmnDriver = new TableColumn(this, SWT.NONE);
		tblclmnDriver.setWidth(100);
		tblclmnDriver.setText("驱动类");

		TableColumn tblclmnUrl = new TableColumn(this, SWT.NONE);
		tblclmnUrl.setWidth(100);
		tblclmnUrl.setText("连接字符串");

		TableColumn tblclmnUser = new TableColumn(this, SWT.NONE);
		tblclmnUser.setWidth(100);
		tblclmnUser.setText("账号");

		TableColumn tblclmnPassword = new TableColumn(this, SWT.NONE);
		tblclmnPassword.setWidth(100);
		tblclmnPassword.setText("密码");

		Menu menuTbl = new Menu(this);
		this.setMenu(menuTbl);

		MenuItem menuItemAdd = new MenuItem(menuTbl, SWT.NONE);
		menuItemAdd.setID(1);
		menuItemAdd.setText("新建");
		menuItemAdd.addSelectionListener(this);

		MenuItem menuItemEdit = new MenuItem(menuTbl, SWT.NONE);
		menuItemEdit.setID(2);
		menuItemEdit.setText("编辑");
		menuItemEdit.addSelectionListener(this);

		MenuItem menuItemDel = new MenuItem(menuTbl, SWT.NONE);
		menuItemDel.setID(3);
		menuItemDel.setText("删除");
		menuItemDel.addSelectionListener(this);

	}

	@Override
	protected void checkSubclass() {
	}

	/**
	 * 外部调用刷新显示
	 * @param cONFIG 
	 */
	public void reflush() {
		this.removeAll();
		for (Connect connect : Operation.CONFIG.connects) {
			TableItem item = new TableItem(this, SWT.NONE);
			item.setText(0, StringUtils.trimToEmpty(connect.getId()));
			item.setText(1, StringUtils.trimToEmpty(connect.getDriver()));
			item.setText(2, StringUtils.trimToEmpty(connect.getUrl()));
			item.setText(3, StringUtils.trimToEmpty(connect.getUser()));
			item.setText(4, StringUtils.trimToEmpty(connect.getPassword()));
		}
	}

	@Override
	public void widgetSelected(SelectionEvent e) {
		int menuId = ((MenuItem) e.widget).getID();
		switch (menuId) {
		case 1: { // 新增
			Connect connect = new ConnectDialog(this.getShell(), SWT.CLOSE).open(null);
			if (connect != null) {
				Operation.CONFIG.connects.add(connect);
				reflush();
			}
			break;
		}
		case 2: { // 编辑
			int index = this.getSelectionIndex();
			if (index > -1) {
				Connect connect = new ConnectDialog(this.getShell(), SWT.CLOSE).open(Operation.CONFIG.connects.get(index));
				if (connect != null) {
					reflush();
				}
			}
			break;
		}
		case 3: { // 删除
			
			break;
		}
		}
		System.out.println();
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) { }
	
}
